package com.easyardemo.view;

import android.Manifest;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.ViewGroup;

import com.easyardemo.R;
import com.easyardemo.app.AppConstant;
import com.easyardemo.tools.util.ToastUtils;
import com.easyarlibrary.app.AppInfo;
import com.easyarlibrary.artools.ArEngine;
import com.easyarlibrary.artools.GLView;
import com.easyarlibrary.artools.ar.CoverAR;
import com.easyarlibrary.util.LogUtil;

import java.util.List;

import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {

    private GLView glView;
    private CoverAR coverAR;
    private static final int RC_CAMERA_PERM = 123;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case AppConstant.HanlderWhat.HANDLE_WHAT_GETBOOKNAME_OK:
                    String currName = msg.obj + "";
                    LogUtil.d("---Handler--->" + currName);
                    ToastUtils.showToast(mContext, "识别到的结果---->" + currName);
                    break;
                default:
                    break;
            }
            return false;
        }
    });

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initAR();
        cameraTask();
//        LoadTargetUtil.getTargetsFormAssets(mContext);
    }

    private void initView() {

        glView = new GLView(mContext);
        //设置不同的识别方式
        coverAR = new CoverAR(handler);
        glView.setBaseAR(coverAR);
        ((ViewGroup) findViewById(R.id.preview)).addView(glView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

    }

    private void initAR() {
        if (!ArEngine.initialize(this, AppInfo.key)) {
            Log.e("HelloAR", "Initialization Failed.");
        }
    }

    @Override
    public void onResume() {
        if (glView != null) {
            glView.onResume();
        }
        if (coverAR != null) {
            coverAR.reSetCurrName();
        }
        super.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        if (glView != null) {
            glView.onPause();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        // Some permissions have been granted
        initView();
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        // Some permissions have been denied
        //这里响应的是除了AppSettingsDialog这个弹出框，剩下的两个弹出框被拒绝或者取消的效果
        ToastUtils.showToast(mContext, "没有该权限，此应用程序可能无法正常工作。打开应用设置界面以修改应用权限");
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                System.exit(0);
            }
        }, 2000);
    }

    private boolean hasCameraPermission() {
        return EasyPermissions.hasPermissions(this, Manifest.permission.CAMERA);
    }

    @AfterPermissionGranted(RC_CAMERA_PERM)
    public void cameraTask() {
        if (hasCameraPermission()) {
            // Have permission, do the thing!
            initView();
        } else {
            // Ask for one permission
            EasyPermissions.requestPermissions(this,
                    "您必须授权开启摄像头权限，才能进行下一步操作",
                    0,
                    Manifest.permission.CAMERA
            );
        }
    }
}


